package zucc.edu.cn.chengchengparttime.LoginAndReg;

import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;

import java.util.ArrayList;

import zucc.edu.cn.chengchengparttime.BaseActivity;
import zucc.edu.cn.chengchengparttime.R;
import zucc.edu.cn.chengchengparttime.User;
import zucc.edu.cn.chengchengparttime.dbHomeHelper;

public class RegisterActivity extends BaseActivity implements View.OnClickListener{
    private Context context;
    private EditText account_in,password_in,sno_in,name_in,school_in,phone_in;
    private Spinner gender_in;
    private Spinner intensiontype1_in;
    private Spinner intensiontype2_in;
    private AppCompatButton btn_register;
    private ImageView goback;
    dbHomeHelper homeHelper; //数据库帮助类
    private SQLiteDatabase mDatabase;// 数据库实例
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_register);
        context=this.getBaseContext();

        initView();
        mDatabase=SQLiteDatabase.openDatabase("/data/user/0/zucc.edu.cn.chengchengparttime/databases/Chengcheng.db",null,SQLiteDatabase.OPEN_READWRITE);
    }

    private void initView() {
        account_in = findViewById(R.id.account_in);
        password_in = findViewById(R.id.password_in);
        sno_in = findViewById(R.id.sno_in);
        name_in = findViewById(R.id.name_in);
        school_in = findViewById(R.id.school_in);
        phone_in = findViewById(R.id.phone_in);
        btn_register = findViewById(R.id.btn_register);
        goback = findViewById(R.id.goback);
        gender_in= findViewById(R.id.gender_in);
        intensiontype1_in= findViewById(R.id.intensiontype1_in);
        intensiontype2_in= findViewById(R.id.intensiontype2_in);
        /**
         * 注册页面能点击的就2个地方
         * top处返回箭头、注册按钮
         */
        goback.setOnClickListener(this);
        btn_register.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.goback://返回登录界面
                Toast.makeText(this.context, "点击了返回" , Toast.LENGTH_SHORT).show();
                //跳转登录界面
                Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
                break;
            case R.id.btn_register://注册按钮
                //获取用户输入的用户名、密码、验证码
                String UserID = account_in.getText().toString().trim();
                String Password = password_in.getText().toString().trim();
                String Sno = sno_in.getText().toString();
                String Name = name_in.getText().toString();
                String School = "浙大城市学院";
                String PhoneNub = phone_in.getText().toString().trim();
                String Gender = gender_in.getSelectedItem().toString();
                String Intensiontype1 = intensiontype1_in.getSelectedItem().toString().trim();
                String Intensiontype2 = intensiontype2_in.getSelectedItem().toString().trim();
                boolean match = true;
                boolean match2 = false;

                //注册验证
                if (!TextUtils.isEmpty(UserID) && !TextUtils.isEmpty(Password)) {
                    //判断两次密码是否一致
//                    if (password1.equals(password2))
                    ArrayList<User> data = dbHomeHelper.getAllData(mDatabase);
                    for (int i = 0; i < data.size(); i++) {
                        User user = data.get(i);
                        if ((UserID.equals(user.getUserID()))) {
                            Toast.makeText(this, "账号已被注册,注册失败", Toast.LENGTH_SHORT).show();
                            match = false;
                            match2 = true;
                            break;
                        } else {
                            match = true;
                        }
                    }
                } else {
                    Toast.makeText(this, "请输入账户和密码,注册失败", Toast.LENGTH_SHORT).show();
                    match = false;
                }
                if(match==true) {
                    if (PhoneNub.length() != 11) {
                        Toast.makeText(this, "请填写正确长度的手机号,注册失败", Toast.LENGTH_SHORT).show();
                        match2 = true;
                    } else {
                        for (int i = 0; i < PhoneNub.length(); i++) {
                            if (!Character.isDigit(PhoneNub.charAt(i))) {
                                Toast.makeText(this, "手机号含有非法字符,注册失败", Toast.LENGTH_SHORT).show();
                                match2 = true;
                            }
                        }
                    }
                    if (Name.length()==0 || Sno.length()==0 || Gender.length()==0) {
                        Toast.makeText(this, "请填写必填的资料,注册失败，请重试", Toast.LENGTH_SHORT).show();
                        match2 = true;
                    } else if (match == true && match2 == false) {
                        dbHomeHelper.add(mDatabase, UserID, Password, Sno, Name, School, Gender, PhoneNub, Intensiontype1, Intensiontype2);
                        Intent intent1 = new Intent(RegisterActivity.this, LoginActivity.class);
                        startActivity(intent1);
                        finish();
                        Toast.makeText(this, "验证通过，注册成功", Toast.LENGTH_SHORT).show();
                    }
                    break;
                }
        }
    }
}
